package com.sanqi.work.manager.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sanqi.work.domain.WorkUser;
import com.sanqi.work.domain.WorkUserAccount;
import com.sanqi.work.manager.IWorkUserAccountManager;
import com.sanqi.work.mapper.WorkUserAccountMapper;
import com.sanqi.work.mapper.WorkUserMapper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class WorkUserAccountManager implements IWorkUserAccountManager {
    @Autowired
    private WorkUserAccountMapper workUserAccountMapper;
    @Autowired
    private WorkUserMapper workUserMapper;

    @Override
    public boolean editUserAccount(String userId, String opCode, String userAccount) {
        try {
            WorkUserAccount account = this.getUserAccountByUserId(userId);
            if(null == account) {
                account = new WorkUserAccount();
                account.setUserId(userId);
                account.setUserAccount(userAccount);
                account.setUserOpCode(opCode);
                this.workUserAccountMapper.insert(account);
            } else {
                account.setUserAccount(userAccount);
                account.setUserOpCode(opCode);
                this.workUserAccountMapper.updateById(account);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
        return true;
    }

    @Override
    public WorkUserAccount getUserAccountByUserId(String userId) {
        LambdaQueryWrapper<WorkUserAccount> query = new LambdaQueryWrapper<>();
        query.eq(WorkUserAccount::getUserId, userId);
        
        List<WorkUserAccount> selectList = this.workUserAccountMapper.selectList(query);
        return CollUtil.isEmpty(selectList) ? null : selectList.get(0);
    }

    @Override
    public List<WorkUserAccount> queryByUserIds(List<String> userIdList) {
        LambdaQueryWrapper<WorkUserAccount> query = new LambdaQueryWrapper<>();
        query.in(WorkUserAccount::getUserId, userIdList);
        return this.workUserAccountMapper.selectList(query);
    }

    @Override
    public int removeByUserId(String userId) {
        LambdaQueryWrapper<WorkUserAccount> query = new LambdaQueryWrapper<>();
        query.eq(WorkUserAccount::getUserId, userId);
        return workUserAccountMapper.delete(query);
    }

    @Override
    public int removeByUserIds(Collection<String> userIds) {
        LambdaQueryWrapper<WorkUserAccount> query = new LambdaQueryWrapper<>();
        query.in(WorkUserAccount::getUserId, userIds);
        return this.workUserAccountMapper.delete(query);
    }

    @Override
    public int saveAccount(WorkUserAccount account) {
        return this.workUserAccountMapper.insert(account);
    }
    
    @Override
    public int updateById(WorkUserAccount account) {
        return this.workUserAccountMapper.updateById(account);
    }

    @Override
    public WorkUser getUserByOpCode(String userOpCode) {
        List<WorkUserAccount> list = this.listByOpCode(userOpCode);
        return getLatestUser(list);
    }
    
    @Override
    public WorkUser getUserByUserAccount(String userAccount) {
        List<WorkUserAccount> list = this.listByUserAccount(userAccount);
        return getLatestUser(list);
    }
    
    @Override
    public List<WorkUserAccount> listByOpCode(String userOpCode) {
        LambdaQueryWrapper<WorkUserAccount> query = new LambdaQueryWrapper<>();
        query.eq(WorkUserAccount::getUserOpCode, userOpCode);
        
        return this.workUserAccountMapper.selectList(query);
    }
    
    @Override
    public List<WorkUserAccount> listByUserAccount(String userAccount) {
        LambdaQueryWrapper<WorkUserAccount> query = new LambdaQueryWrapper<>();
        query.eq(WorkUserAccount::getUserAccount, userAccount);
        
        return this.workUserAccountMapper.selectList(query);
    }
    
    private WorkUser getLatestUser(List<WorkUserAccount> list) {
        if(CollUtil.isEmpty(list)) {
            return null;
        }
        if(list.size() == 1) {
            WorkUserAccount workUserAccount = list.get(0);
            String userId = workUserAccount.getUserId();
            return this.workUserMapper.selectById(userId);
        }
        String selUserId = null;
        Date date = null;
        for (WorkUserAccount workUserAccount : list) {
            String userId = workUserAccount.getUserId();
            Date userDate = workUserAccount.getUpdateTime() == null ? workUserAccount.getCreateTime() : workUserAccount.getUpdateTime();
            if(DateUtil.compare(userDate, date) > 0) {
                date = userDate;
                selUserId = userId;
            }
        }
        return null == selUserId ? null : this.workUserMapper.selectById(selUserId);
    }

    @Override
    public List<WorkUserAccount> listByUserIds(Collection<String> userIds) {
        if(CollUtil.isEmpty(userIds)) {
            return CollUtil.newArrayList();
        }
        LambdaQueryWrapper<WorkUserAccount> query = new LambdaQueryWrapper<>();
        query.in(WorkUserAccount::getUserId, userIds);
        return this.workUserAccountMapper.selectList(query);
    }
}
